﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Net;
using System.Xml;
using System.Threading;

namespace Spider
{
    public partial class FrmUpdate : Form
    {
        public FrmUpdate()
        {
            InitializeComponent();
        }
        SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "NetSpider");
        private void FrmUpdate_Load(object sender, EventArgs e)
        {
            checkUpdate();
        }
        private void FrmUpdate_Shown(object sender, EventArgs e)
        {
            app.DownloadFile(this.lblTip, this.progressBar1);
        }

        public void checkUpdate()
        {
            app.UpdateFinish += new UpdateState(app_UpdateFinish);
            try
            {
                if (!app.IsUpdate)
                {
                    MessageBox.Show("您运行的程序已是最新版本，无需更新！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    return;
                }
                if (MessageBox.Show("检查到新版本，是否更新？", "程序更新", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Thread update = new Thread(new ThreadStart(app.Update));
                    //update.Start();
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
        }

        void app_UpdateFinish()
        {
            MessageBox.Show("更新完成，程序自动重启，请稍等...！", "更新完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
            string temp = Environment.CurrentDirectory + "\\Temp.exe";
            if (File.Exists(temp))
            {
                System.Diagnostics.Process.Start(temp);
            }
            else
            {
                MessageBox.Show("升级后的程序启动失败，请联系作者...");
            }
        }
    }
}
